from flask import current_app
from blog import create_app, db
from flask_testing import TestCase
import unittest
from factory import fuzzy
from factory.alchemy import SQLAlchemyModelFactory
from blog.appone import schema
from blog.appone import models

class RandomPostFactory(SQLAlchemyModelFactory):
    class Meta:
        model = models.Post
        sqlalchemy_session = db.session

    title = fuzzy.FuzzyText(prefix='factoryTest_', length=12)
    content = fuzzy.FuzzyText(length=30)


class apponeTestCase(TestCase):

    def create_app(self):
        app = create_app({
            'TESTING': True,
            'SQLALCHEMY_DATABASE_URI': 'postgresql://:@localhost/learn2',
            'SQLALCHEMY_TRACK_MODIFICATIONS': True
        })
        return app


    def setUp(self):
        db.create_all()


    def tearDown(self):
        db.session.remove()
        db.drop_all()


    def test_app_exits(self):
        self.assertFalse(current_app is None)


    def test_app_is_testing(self):
        self.assertTrue(current_app.config['TESTING'])

    def test_db(self):
            p = RandomPostFactory()
            self.assertEqual([p], db.session.query(models.Post).all())

    def test_get(self):
            result = self.client.get('/api/posts')
            self.assert200(result)

